package com.orangelife.webservice.service.propFeeDirectBill.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.offset.IOffsetService;
import com.orangelife.service.propFeeDirectBill.IPropFeeDirectBillService;
import com.orangelife.service.propFeeDirectBill.model.PropFeeDirectBillDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.utils.PropertyUtils;
import com.orangelife.utils.StringUtils;
import com.orangelife.webservice.base.BaseService;
import com.orangelife.webservice.base.PageUtils;
import com.orangelife.webservice.service.propFeeDirectBill.IPropFeeDirectBillWebService;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation 2015. All Rights Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * propFeeDirectBill类 的WebService基本作业
 * 
 * @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-11-24 14:14:12
 * ==============================================================
 */
@Service("webservice_propFeeDirectBill")
public class PropFeeDirectBillWebServiceImpl extends BaseService implements
		IPropFeeDirectBillWebService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Resource(name = "propFeeDirectBillService")
	private IPropFeeDirectBillService propFeeDirectBillService;
	@Resource(name = "offsetService")
	private IOffsetService offsetService;

	@Override
	public Response addPropFeeDirectBill(HttpServletRequest servletRequest,
			PropFeeDirectBillDto propFeeDirectBill) throws Exception {

		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}

		propFeeDirectBill.setCreateuserID(pageUtils.getBaseSession()
				.getUserID());
		propFeeDirectBill.setReceiverID(pageUtils.getBaseSession().getUserID());
		propFeeDirectBill.setReceiver(StringUtils.isBlank(pageUtils
				.getBaseSession().getTrueName()) ? pageUtils.getBaseSession()
				.getLoginName() : pageUtils.getBaseSession().getTrueName());
		propFeeDirectBill.setAccessToken(pageUtils.getBaseSession().getKey());
		int result = propFeeDirectBillService
				.addPropFeeDirectBills(propFeeDirectBill);
		if (!(result > 0)) {
			return this.abnormalResp(CodeConst.BIS_CODE_13006,
					InfoConst.BIS_INFO_13006);
		}
		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "添加实体成功");
		Map<String, Object> maps = new HashMap<String, Object>();
		maps.put("directBillID", propFeeDirectBill.getDirectBillID());
		return this.normalResp(maps);
	}

	@Override
	public Response editPropFeeDirectBill(HttpServletRequest servletRequest,
			PropFeeDirectBillDto propFeeDirectBill) throws Exception {
		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}
		/************************************************
		 * 输入参数ID检查
		 ************************************************/
		Response error = this.validateUUId(propFeeDirectBill.getDirectBillID(),
				"directBillID");
		if (error != null) {
			return error;
		}

		/************************************************
		 * 输入参数检查
		 ************************************************/
		// TODO;

		propFeeDirectBill.setEdituserID(pageUtils.getBaseSession().getUserID());
		int result = propFeeDirectBillService
				.editPropFeeDirectBill(propFeeDirectBill);
		if (!(result > 0)) {
			return this.abnormalResp(CodeConst.BIS_CODE_13006,
					InfoConst.BIS_INFO_13006);
		}
		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "修改实体成功");

		return this.normalResp();
	}

	@Override
	public Response deletePropFeeDirectBill(HttpServletRequest servletRequest,
			String directBillID) throws Exception {
		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}
		/************************************************
		 * 输入参数ID检查
		 ************************************************/
		Response error = this.validateUUId(directBillID, "directBillID");
		if (error != null) {
			return error;
		}
		int result = propFeeDirectBillService
				.deletePropFeeDirectBillByLogic(directBillID);
		if (!(result > 0)) {
			return this.abnormalResp(CodeConst.BIS_CODE_13006,
					InfoConst.BIS_INFO_13006);
		}
		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "删除实体成功");

		return this.normalResp();
	}

	@Override
	public Response findPropFeeDirectBillByID(
			HttpServletRequest servletRequest, String directBillID)
			throws Exception {

		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}
		/************************************************
		 * 输入参数ID检查
		 ************************************************/
		Response error = this.validateUUId(directBillID, "directBillID");
		if (error != null) {
			return error;
		}
		PropFeeDirectBillDto propFeeDirectBill = propFeeDirectBillService
				.findPropFeeDirectBillByID(directBillID);

		Map<String, Object> maps = new HashMap<String, Object>();

		if (propFeeDirectBill != null) {

		}

		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "查询实体成功");
		return this.normalResp(propFeeDirectBill);
	}

	@Override
	public Response getAllPropFeeDirectBill(HttpServletRequest servletRequest,
			String commID, String apartmentType, String apartmentNum,
			String isBack, String receiptNum, String invoiceNum,
			String startTime, String endTime, String payTypes, String feeTypes,
			String billType, String tsCreate, String pageIndex, String pageSize)
			throws Exception {

		PageUtils pageUtils = this.validatePageAndSetPage(servletRequest,
				pageIndex, pageSize);
		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}

		if (!StringUtils.isBlank(tsCreate)) {
			StringUtils.isTime(tsCreate);
			tsCreate = String.valueOf(Long.parseLong(tsCreate) / 1000);
		}
		/************************************************
		 * 查询列表
		 ************************************************/
		List<Map<String, Object>> list = propFeeDirectBillService
				.findPropFeeDirectBillByPage(isBack, commID, apartmentType,
						apartmentNum, receiptNum, invoiceNum, startTime,
						endTime, payTypes, feeTypes, tsCreate == null ? null
								: tsCreate, billType == null ? null : billType,
						pageUtils.getiPageIndex(), pageUtils.getiPageSize());

		/************************************************
		 * 返回结果设定
		 ************************************************/
		Long total = ((Page<Map<String, Object>>) list).getTotal();

		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginName() + new Date()
				+ "查询实体成功");

		return this.normalResp(pageUtils.getiPageIndex(),
				pageUtils.getiPageSize(), total,
				PropertyUtils.turnToEntity(list));
	}

	@Override
	public Response bauthFindPropFeeDirectBill(
			HttpServletRequest servletRequest, List<String> propFeeDirectBill)
			throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			/************************************************
			 * 输入参数ID检查
			 ************************************************/
			if (propFeeDirectBill == null || propFeeDirectBill.size() <= 0) {
				logger.error(InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
				return this.abnormalResp(CodeConst.SYS_CODE_1001,
						InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
			}

			map.put("pageEntity", propFeeDirectBillService
					.bauthfindPropFeeDirectBill(propFeeDirectBill));
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response bauthFindPropFeeDirectBills(
			HttpServletRequest servletRequest,
			Map<String, Object> propFeeDirectBill) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			/************************************************
			 * 输入参数ID检查
			 ************************************************/
			if (propFeeDirectBill == null || propFeeDirectBill.size() <= 0) {
				logger.error(InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
				return this.abnormalResp(CodeConst.SYS_CODE_1001,
						InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
			}

			map.put("pageEntity",
					PropertyUtils.turnToEntity(propFeeDirectBillService
							.bauthfindPropFeeDirectBillByMap(propFeeDirectBill)));
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response clearCache(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		propFeeDirectBillService.clearCache();
		return this.normalResp();
	}

	@Override
	public Response returnBackBill(HttpServletRequest servletRequest,
			PropFeeDirectBillDto propFeeDirectBill) throws Exception {
		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}
		/************************************************
		 * 输入参数ID检查
		 ************************************************/

		/************************************************
		 * 输入参数检查
		 ************************************************/
		// TODO;
		propFeeDirectBill.setCreateuserID(pageUtils.getBaseSession()
				.getUserID());
		propFeeDirectBill.setReceiverID(pageUtils.getBaseSession().getUserID());
		propFeeDirectBill.setReceiver(StringUtils.isBlank(pageUtils
				.getBaseSession().getTrueName()) ? pageUtils.getBaseSession()
				.getLoginName() : pageUtils.getBaseSession().getTrueName());
		propFeeDirectBill.setEdituserID(pageUtils.getBaseSession().getUserID());
		propFeeDirectBill.setBackTime(new Date());
		propFeeDirectBill.setAccessToken(pageUtils.getBaseSession().getKey());
		int result = propFeeDirectBillService.returnBackBill(propFeeDirectBill);
		if (!(result > 0)) {
			return this.abnormalResp(CodeConst.BIS_CODE_13006,
					InfoConst.BIS_INFO_13006);
		}
		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "修改实体成功");

		return this.normalResp();
	}

	@Override
	public Response turnAccountBill(HttpServletRequest servletRequest,
			PropFeeDirectBillDto propFeeDirectBill) throws Exception {
		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}
		/************************************************
		 * 输入参数ID检查
		 ************************************************/

		/************************************************
		 * 输入参数检查
		 ************************************************/
		// TODO;
		propFeeDirectBill.setCreateuserID(pageUtils.getBaseSession()
				.getUserID());
		propFeeDirectBill.setBackTime(new Date());
		propFeeDirectBill.setReceiverID(pageUtils.getBaseSession().getUserID());
		propFeeDirectBill.setReceiver(StringUtils.isBlank(pageUtils
				.getBaseSession().getTrueName()) ? pageUtils.getBaseSession()
				.getLoginName() : pageUtils.getBaseSession().getTrueName());
		propFeeDirectBill.setEdituserID(pageUtils.getBaseSession().getUserID());
		int result = propFeeDirectBillService
				.turnAccountBill(propFeeDirectBill);
		final String commID = propFeeDirectBill.getCommID();
		final String apartmentID = propFeeDirectBill.getApartmentID();

		final String userID = propFeeDirectBill.getReceiverID();
		final String userName = propFeeDirectBill.getReceiver();
		if (!(result > 0)) {
			return this.abnormalResp(CodeConst.BIS_CODE_13006,
					InfoConst.BIS_INFO_13006);
		}

		try {

			Thread thread = new Thread(new Runnable() {

				@Override
				public void run() {
					try {

						Thread.sleep(1000);

						offsetService.autoOffset(commID, apartmentID, userID,
								userName);
					} catch (Exception e) {
						logger.error(e.getMessage());
					}

				}
			});
			thread.start();

		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "修改实体成功");

		return this.normalResp();
	}

	@Override
	public Response getTotalAmountDirectBill(HttpServletRequest servletRequest,
			String commID, String apartmentType, String apartmentNum,
			String isBack, String receiptNum, String invoiceNum,
			String startTime, String endTime, String payTypes, String feeTypes,
			String billType, String tsCreate) throws Exception {

		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}

		if (!StringUtils.isBlank(tsCreate)) {
			StringUtils.isTime(tsCreate);
			tsCreate = String.valueOf(Long.parseLong(tsCreate) / 1000);
		}
		/************************************************
		 * 查询列表
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("totalAmount", 0.00);
		map.put("totalDeductMoney", 0.00);
		map = propFeeDirectBillService.getTotalAmount(commID, apartmentType,
				apartmentNum, receiptNum, invoiceNum, startTime, endTime,
				payTypes, feeTypes, tsCreate == null ? null : tsCreate,
				billType == null ? null : billType);

		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "查询实体成功");
		return this.normalResp(map);

	}

	@Override
	public Response revokePropFeeDirectBillByID(
			HttpServletRequest servletRequest, Map<String, Object> map)
			throws Exception {
		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}
		/************************************************
		 * 输入参数ID检查
		 ************************************************/

		/************************************************
		 * 输入参数检查
		 ************************************************/
		// TODO;

		PropFeeDirectBillDto propFeeDirectBill = propFeeDirectBillService
				.findPropFeeDirectBillByID(map.get("directBillID").toString());

		propFeeDirectBill.setCanleMan(StringUtils.isBlank(pageUtils
				.getBaseSession().getTrueName()) ? pageUtils.getBaseSession()
				.getLoginName() : pageUtils.getBaseSession().getTrueName());
		propFeeDirectBill.setCanleManID(pageUtils.getBaseSession().getUserID());
		propFeeDirectBill.setCanleTime(new Date());

		int result = propFeeDirectBillService
				.revokePropFeeDirect(propFeeDirectBill);
		if (!(result > 0)) {
			return this.abnormalResp(CodeConst.BIS_CODE_13006,
					InfoConst.BIS_INFO_13006);
		}
		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "修改实体成功");

		return this.normalResp();
	}

	@Override
	public Response canclePropFeeDirectBill(HttpServletRequest servletRequest,
			Map<String, Object> propFeeDirectBill) throws Exception {

		PageUtils pageUtils = this.validatePageAndSetPage(servletRequest,
				propFeeDirectBill.get("pageIndex").toString(),
				propFeeDirectBill.get("pageSize").toString());

		/************************************************
		 * 查询列表
		 ************************************************/
		List<Map<String, Object>> list = propFeeDirectBillService
				.canclePropFeeDirectBill(propFeeDirectBill,
						pageUtils.getiPageIndex(), pageUtils.getiPageSize());
		/************************************************
		 * 返回结果设定
		 ************************************************/
		Long total = ((Page<Map<String, Object>>) list).getTotal();
		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginName() + new Date()
				+ "查询实体成功");

		return this.normalResp(pageUtils.getiPageIndex(),
				pageUtils.getiPageSize(), total,
				PropertyUtils.turnToEntity(list));
	}
}
